
/**
 * 获取用户经纬度信息，外加用户经纬度信息权限处理
 * loaction = { latitude: res.latitude, longitude: res.longitude}
 */
export function getUserLocation() {
  return new Promise((resolve, reject) => {
    /**
     * 用户位置信息权限处理
     */
    function getUserJurisdiction() {
      uni.getSetting({
        success: (res) => {
          // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
          // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
          // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
          // 拒绝授权后再次进入重新授权
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
            // log('authSetting:status:拒绝授权后再次进入重新授权', res.authSetting['scope.userLocation'])
            uni.showModal({
              title: "授权",
              content: "为了给您推荐附近的汽车门店，我们需要获取您当前的地理位置。",
              success(res) {
                if (!res.confirm) return reject()
                uni.openSetting({
                  success: function (dataAu) {
                    // log('dataAu:success', dataAu)
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      //再次授权，调用wx.getLocation的API
                      getWxLocation(dataAu)
                    } else {
                      reject()
                    }
                  }
                })
              },
              fail() {
                reject()
              }
            })
          }
          // 初始化进入，未授权
          else if (res.authSetting['scope.userLocation'] == undefined) {
            // log('authSetting:status:初始化进入，未授权', res.authSetting['scope.userLocation'])
            //调用wx.getLocation的API
            getWxLocation(res)
          }
          // 已授权
          else if (res.authSetting['scope.userLocation']) {
            // log('authSetting:status:已授权', res.authSetting['scope.userLocation'])
            //调用wx.getLocation的API
            getWxLocation(res)
          } else {
            //未知授权状态,作为失败处理
            reject()
          }
        }
      })
    }
    /**
     * 微信获得经纬度
     * {latitude: res.latitude,longitude: res.longitude}
     */
    function getWxLocation(userLocation) {
      uni.getLocation({
        type: "wgs84",
        success: function (res) {
          // log('getLocation:success', res)
          const loaction = {
            latitude: res.latitude,
            longitude: res.longitude
          }
          resolve(loaction)
        },
        fail: function (res) {
          // log('getLocation:fail', res)
          if (res.errMsg === 'getLocation:fail:auth denied') {
            reject()
            return
          }
          if (!userLocation || userLocation.authSetting['scope.userLocation'] === false) {
            getUserJurisdiction()
          } else if (userLocation.authSetting['scope.userLocation'] == null) {
            reject()
          } else if (userLocation.authSetting['scope.userLocation']) {
            uni.showModal({
              title: "温馨提示",
              content: "请在系统设置中打开定位服务",
              success(res) {
                if (!res.confirm) return reject()
                resolve(true)
              },
              fail() {
                reject()
              }
            })
          } else {
            reject()
          }
        }
      })
    }
    getWxLocation()
  })
}